﻿using iTransact.Requests;
using iTransact.Responses;
using Microsoft.Http;
using iTransact.Xml;

namespace iTransact
{
	public static class PaymentProcessor
	{
		
		/// <summary>
		/// Charge Credit Card
		/// </summary>
		public static Response ChargeCreditCard(ChargeRequest request)
		{
			return Charge(request);
		}

		/// <summary>
		/// Charge Reocurring Card
		/// </summary>
		public static Response ChargeReocurringCreditCard(ReocurringRequest request)
		{
			return Charge(request);
		}

		// Wrapper for Auth Request
		static Response Charge(Request request)
		{
			var xml = string.Empty;

			if (request.GetType() == typeof(ChargeRequest))
				xml = AuthTransactionXml.Generate(request);

			if (request.GetType() == typeof(ReocurringRequest))
				xml = AuthTransactionXml.Generate(request);

			var http = new HttpClient();
			var response = http.Post("https://secure.paymentclearing.com/cgi-bin/rc/xmltrans2.cgi", HttpContent.Create(xml, "text/xml"));
			return ProcessResponse.Process(response.Content.ReadAsStream());

		}
	}
}
